#include "uhf_rfid_reader.h"
#include "ui_uhf_rfid_reader.h"
#include "structinterface.h"
#include <QMessageBox>
#include <QMutexLocker>



unsigned char g_btReadId = 0x01;

UHF_RFID_Reader::UHF_RFID_Reader(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::UHF_RFID_Reader)
{
    ui->setupUi(this);

    t_connected = false;
    t_TcpSocket = new QTcpSocket(this);

    connect(t_TcpSocket, SIGNAL(connected()), this, SLOT(On_tTcpSocket_connected()) );


    m_TcpSocket = new QTcpSocket(this);
    c_pDealData = new DealMessage;
    m_btcpInit = true;

    //m_readerMethod = new ReaderMethod(this);
    // m_readerMethod = new ReaderMethod(m_TcpSocket);
    m_readerMethod = new ReaderMethod();
    InitFreqPoint();
    InitMapRssi();
    InitServer();
    InitRadio();
    InitButton();
    InitWidget();
    LockInterface();



    m_isInventory = true;
    m_isInventoryFast = true;
    m_alysisMsg = new AnalysisMsg;

    freqregion = 0xFF;

    m_logNum = 0;

    //g_btReadId = 0x01;
    //解析数据操作是否完成
    m_dealIsOver = true;
    //盘存操作前，需要先设置工作天线，用于标识当前是否在执行盘存操作
    m_Inventory = false;
    //标识是否统计命令执行时间，当前仅盘存命令需要统计实际
    m_bReckonTime = false;
    //实时盘存锁定操作
    m_bLockTab = false;
    //ISO18000标签连续盘存标识
    m_bContinue = false;
    //记录ISO18000标签循环写-入次数
    m_nLoopTimes = 0;
    //记录ISO18000标签写入字符数
    m_nBytes = 0;
    //记录ISO18000标签已经循环写入次数
    m_nLoopedTimes = 0;
    //实时盘存次数
    m_nTotal = 0;
    //列表更新频率
    m_nRealRate = 20;
    //记录快速轮询天线参数
    memset(m_btAryData, 0, 10);
    //记录快速轮询总次数
    m_nSwitchTotal = 0;
    m_nSwitchTime = 0;
    m_nReceiveFlag = 0;
    //设置工作天线
    m_nWorkAnt = 5;
    //已读取标签数量
    m_nTagNum1 = 0;
    m_nTagNum2 = 0;
    m_nTagNum3 = 0;

    m_nTotalRead1 = 0;
    m_nBackNum1 = 0;
    m_lTotalTime1 = 0;
    m_nTotalRead2 = 0;
    m_nBackNum2 = 0;
    m_lTotalTime2 = 0;

    m_bFastInventory = true;

    m_btDrmMode = 0xFF;
    //m_compareMsgThread = new CompareMsgThread();



    m_profileId = 0x00;
    m_beeper = 0x00;

    //m_labelList << " ";
    //    qRegisterMetaType<unsigned char*>("unsigned char*");
    //    qRegisterMetaType<unsigned char>("unsigned char");
    //    qRegisterMetaType<InventoryCount>("InventoryCount");
    //    qRegisterMetaType<TagReadBack>("TagReadBack");
    //    qRegisterMetaType<EpcData>("EpcData");
    //    qRegisterMetaType<FreqRegionDefault>("FreqRegionDefault");
    //    qRegisterMetaType<FreqRegionCustom>("FreqRegionCustom");


    connect( m_TcpSocket, SIGNAL(connected()), this, SLOT(OnConnected_Tcp()) );
    connect( m_TcpSocket, SIGNAL(connected()), this, SLOT(UnlockInterface()) );
    //  connect( m_TcpSocket, SIGNAL(readyRead()), this, SLOT(ReadMessage()) );

    connect( m_alysisMsg, SIGNAL(Signal_ShowFirewareVersion(char*,int)), this, SLOT(ShowLineEditVersion(char*, int)) );
    connect( m_alysisMsg, SIGNAL(Signal_ShowReaderTemp(char*,int)), this, SLOT(ShowLineEditTemp(char*, int)) );
    connect( m_alysisMsg, SIGNAL(Signal_ShowReaderIdentifier(QString)), this, SLOT(ShowLineEditIdentifier(QString)) );
    connect( m_alysisMsg, SIGNAL(Signal_ShowOutputPower(int)), this, SLOT(ShowLineEditOutputPower(int)) );
    connect( m_alysisMsg, SIGNAL(Signal_ShowInventoryData(InventoryCount)), this, SLOT(ShowInventory(InventoryCount)) );
    connect( m_alysisMsg, SIGNAL(Signal_ShowReadTagData(TagReadBack)), this, SLOT(ShowReadTagData(TagReadBack)) );
    connect( m_alysisMsg, SIGNAL(Signal_ShowEpcBuffer(EpcData)), this, SLOT(ShowEpcBuffer(EpcData)) );
    connect( m_alysisMsg, SIGNAL(Signal_ShowFastInventoryData(int,int)), this, SLOT(ShowFastInventory(int,int)) );
    //  connect( c_pDealData, SIGNAL(Signal_ShowReadTagData(ReadTagData)), this, SLOT(ShowReadTagData(ReadTagData)) );
    connect( m_alysisMsg, SIGNAL(Signal_ShowFastEpcBuffer(EpcData)), this, SLOT(ShowFastEpcBuffer(EpcData)) );
    connect( m_alysisMsg, SIGNAL(Signal_DrmModeGet(unsigned char)), this, SLOT(DrmModeGet(unsigned char)) );
    connect( m_alysisMsg, SIGNAL(Signal_ReadWorkAnt(unsigned char)), this, SLOT(ReadWorkAnt(unsigned char)) );
    connect( m_alysisMsg, SIGNAL(Signal_GetAntDetector(unsigned char)), this, SLOT(AntDetector(unsigned char)) );
    connect( m_alysisMsg, SIGNAL(Signal_ReturnLoss(unsigned char)), this, SLOT(ReturnLoss(unsigned char)) );
    connect( m_alysisMsg, SIGNAL(Signal_FreqRegion(FreqRegionDefault)), this, SLOT(FreqRegion(FreqRegionDefault)) );
    connect( m_alysisMsg, SIGNAL(Signal_FreqRegionCustom(FreqRegionCustom,int)), this, SLOT(CustomFreqRegion(FreqRegionCustom,int)) );
    connect( m_alysisMsg, SIGNAL(Signal_RfLinkProfile(unsigned char)), this, SLOT(RfLinkProfile(unsigned char)) );
    connect( m_alysisMsg, SIGNAL(Signal_GetAccessEpc(unsigned char*,int)), this, SLOT(GetAccessEpc(unsigned char*,int)) );
    connect( m_alysisMsg, SIGNAL(Signal_ReadGPIO(unsigned char,unsigned char)), this, SLOT(ShowGPIOValue(unsigned char,unsigned char)) );
    //connect( c_pDealData, SIGNAL(), this, SLOT() );

    // connect( m_compareMsgThread, SIGNAL(Signal_ShowTxtLog(string)), this, SLOT(ShowTxtLog(string)) );

    //m_compareMsgThread->start();
}


UHF_RFID_Reader::~UHF_RFID_Reader()
{
    //on_btnStopInventory_clicked();
    //on_btnStopFastInventory_clicked();
    // on_btnDisconnect_Tcp_clicked();
    delete ui;
    delete m_readerMethod;
    //  delete m_TcpSocket;
    // delete m_readerMethod;

}

void UHF_RFID_Reader::InitMapRssi()
{
    unsigned char ch = 0x62;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-31")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-32")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-33")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-34")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-35")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-36")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-37")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-38")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-39")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-40")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-41")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-42")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-43")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-44")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-45")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-46")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-47")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-48")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-49")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-50")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-51")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-52")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-53")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-54")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-55")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-56")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-57")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-58")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-59")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-60")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-61")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-62")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-63")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-64")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-65")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-66")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-67")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-68")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-69")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-70")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-71")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-72")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-73")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-74")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-75")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-76")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-77")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-78")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-79")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-80")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-81")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-82")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-83")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-84")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-85")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-86")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-87")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-88")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-89")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-90")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-91")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-92")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-93")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-94")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-95")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-96")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-97")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-98")); ch--;
    m_mapRssi.insert(pair<unsigned char, string>(ch, "-99")); ch--;
}



void UHF_RFID_Reader::InitFreqPoint()
{
    unsigned char ch = 0x00;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "865.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "865.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "866.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "866.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "867.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "867.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "868.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "902.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "902.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "903.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "903.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "904.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "904.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "905.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "905.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "906.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "906.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "907.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "907.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "908.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "908.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "909.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "909.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "910.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "910.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "911.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "911.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "912.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "912.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "913.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "913.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "914.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "914.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "915.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "915.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "916.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "916.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "917.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "917.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "918.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "918.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "919.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "919.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "920.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "920.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "921.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "921.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "922.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "922.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "923.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "923.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "924.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "924.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "925.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "925.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "926.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "926.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "927.00")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "927.50")); ch++;
    m_mapFreq.insert(pair<unsigned char, string>(ch, "928.00")); ch++;
}

// 界面相关初始化
void UHF_RFID_Reader::InitServer()
{
    ui->lineEdit_IpAddress->setText("192.168.0.178");
    // ui->lineEdit_IpAddress->setText("169.254.250.64");
    ui->lineEdit_ServerPort->setText("4001");
    // ui->lineEdit_ServerPort->setText("8080");
}

void UHF_RFID_Reader::InitRadio()
{
    //连接方式Radio初始化
    m_conncet_radios = new QButtonGroup(this);
    m_conncet_radios->addButton(ui->radioConncet_Com, 0);
    m_conncet_radios->addButton(ui->radioConncet_Tcp, 1);
    ui->radioConncet_Tcp->setChecked(true);
    connect(m_conncet_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_Connect_Radios_Clicked(int)) );
    On_Connect_Radios_Clicked(1);

    m_freqregion_radios = new QButtonGroup(this);
    m_freqregion_radios->addButton(ui->rbBtn_FCC, 0);
    m_freqregion_radios->addButton(ui->rbBtn_ETSI, 1);
    m_freqregion_radios->addButton(ui->rbBtn_CHN, 2);
    ui->rbBtn_FCC->setChecked(false);
    connect(m_freqregion_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_Freq_Radios_Clicked(int)) );

    m_linkprofile_radios = new QButtonGroup(this);
    m_linkprofile_radios->addButton(ui->rbl_0, 0);
    m_linkprofile_radios->addButton(ui->rbl_1, 1);
    m_linkprofile_radios->addButton(ui->rbl_2, 2);
    m_linkprofile_radios->addButton(ui->rbl_3, 3);
    connect(m_linkprofile_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_RfLink_Radios_Clicked(int)) );
    On_RfLink_Radios_Clicked(0);

    m_readtag_radios = new QButtonGroup(this);
    m_readtag_radios->addButton(ui->read_pass, 0);
    m_readtag_radios->addButton(ui->read_epc, 1);
    m_readtag_radios->addButton(ui->read_tid, 2);
    m_readtag_radios->addButton(ui->read_user, 3);
    connect( m_readtag_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_ReadTag_Radios_Clicked(int)) );
    On_ReadTag_Radios_Clicked(0);

    m_beeper_radios = new QButtonGroup(this);
    m_beeper_radios->addButton(ui->radioButton_Beeper1, 0);
    m_beeper_radios->addButton(ui->radioButton_Beeper2, 1);
    m_beeper_radios->addButton(ui->radioButton_Beeper3, 2);
    connect( m_beeper_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_Beeper_Radios_Clicked(int)) );

    m_lockMembank_radios = new QButtonGroup(this);
    m_lockMembank_radios->addButton(ui->radioButton_19, 0);
    m_lockMembank_radios->addButton(ui->radioButton_20, 1);
    m_lockMembank_radios->addButton(ui->radioButton_21, 2);
    m_lockMembank_radios->addButton(ui->radioButton_22, 3);
    m_lockMembank_radios->addButton(ui->radioButton_27, 4);
    connect( m_lockMembank_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_lockMembank_Radios_Clicked(int)) );
    On_lockMembank_Radios_Clicked(0);

    m_locktype_radios = new QButtonGroup(this);
    m_locktype_radios->addButton(ui->lock_open, 0);
    m_locktype_radios->addButton(ui->lock_lock, 1);
    m_locktype_radios->addButton(ui->lock_alopen, 2);
    m_locktype_radios->addButton(ui->lock_allock, 3);
    connect( m_locktype_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_locktype_Radios_Clicked(int)) );
    On_locktype_Radios_Clicked(0);

    m_gpio1_radios = new QButtonGroup(this);
    m_gpio1_radios->addButton(ui->gpio1_high, 0);
    m_gpio1_radios->addButton(ui->gpio1_low, 1);
    connect( m_gpio1_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_gpio1_Radios_Clicked(int)) );


    m_gpio2_radios = new QButtonGroup(this);
    m_gpio2_radios->addButton(ui->gpio2_high, 0);
    m_gpio2_radios->addButton(ui->gpio2_low, 1);
    connect( m_gpio2_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_gpio2_Radios_Clicked(int)) );

    m_gpio3_radios = new QButtonGroup(this);
    m_gpio3_radios->addButton(ui->gpio3_high, 0);
    m_gpio3_radios->addButton(ui->gpio3_low, 1);
    connect( m_gpio3_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_gpio3_Radios_Clicked(int)) );

    m_gpio4_radios = new QButtonGroup(this);
    m_gpio4_radios->addButton(ui->gpio4_high, 0);
    m_gpio4_radios->addButton(ui->gpio4_low, 1);
    connect( m_gpio4_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_gpio4_Radios_Clicked(int)) );


}

void UHF_RFID_Reader::InitButton()
{
    ui->btnStopInventory->setEnabled(false);
    ui->btnStopFastInventory->setEnabled(false);

    ui->ckBox_Session->setCheckState(Qt::Unchecked);
    ui->label_SessionId->setEnabled(false);
    ui->label_InventoriedFlag->setEnabled(false);

    m_drm_radios = new QButtonGroup(this);
    m_drm_radios->addButton(ui->rbBtn_DrmOpen, 0);
    m_drm_radios->addButton(ui->rbBtn_DrmClose, 1);
    connect(m_drm_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_Drm_Radios_Clicked(int)) );

    //ui->btnReset->setEnabled(false);
    ui->cmbBox_SessionID->addItem(QWidget::tr("A0"));
    ui->cmbBox_SessionID->addItem(QWidget::tr("A1"));
    ui->cmbBox_SessionID->addItem(QWidget::tr("A2"));
    ui->cmbBox_SessionID->addItem(QWidget::tr("A3"));
    ui->cmbBox_SessionID->setEnabled(false);

    ui->cmbBox_InventFlag->addItem(QWidget::tr("A"));
    ui->cmbBox_InventFlag->addItem(QWidget::tr("B"));
    ui->cmbBox_InventFlag->setEnabled(false);

    //初始化天线
    ui->ckBox_Ant1->setCheckState(Qt::Unchecked);
    ui->ckBox_Ant2->setCheckState(Qt::Unchecked);
    ui->ckBox_Ant3->setCheckState(Qt::Unchecked);
    ui->ckBox_Ant4->setCheckState(Qt::Unchecked);

    //天线端口波损耗
    QStringList fonts;
    fonts << "865.00" << "865.50" << "866.00" << "866.50" << "867.00" << "867.50" << "868.00" <<
             "902.00" << "902.50" << "903.00" << "903.50" << "904.00" << "904.50" << "905.00" <<
             "905.50" << "906.00" << "906.50" << "907.00" << "907.50" << "908.00" << "908.50" <<
             "909.00" << "909.50" << "910.00" << "910.50" << "911.00" << "911.50" << "912.00" <<
             "912.50" << "913.00" << "913.50" << "914.00" << "914.50" << "915.00" << "915.50" <<
             "916.00" << "916.50" << "917.00" << "917.50" << "918.00" << "918.50" << "919.00" <<
             "919.50" << "920.00" << "920.50" << "921.00" << "921.50" << "922.00" << "922.50" <<
             "923.00" << "923.50" << "924.00" << "924.50" << "925.00" << "925.50" << "926.00" <<
             "926.50" << "927.00" << "927.50" << "928.00";
    ui->cmbBox_RL->addItems(fonts);
    ui->cmbBox_RL->setCurrentText("915.00");

    //切换当前工作天线
    QStringList combo_ants;
    combo_ants << "天线 1" << "天线 2" << "天线 3" << "天线 4";
    ui->cmbBox_ant->addItems(combo_ants);
    //ui->cmbBox_ant->setCurrentText(" ");

    ui->lineEdit_18->setText(QString("1"));
    ui->lineEdit_19->setText(QString("1"));
    ui->lineEdit_20->setText(QString("1"));
    ui->lineEdit_21->setText(QString("1"));
    ui->lineEdit_22->setText(QString("0"));
    ui->lineEdit_23->setText(QString("10"));

    QStringList ant;
    ant << "天线1" << "天线2" << "天线3" << "天线4" << "不选";
    ui->comboBox_12->addItems(ant);
    ui->comboBox_13->addItems(ant);
    ui->comboBox_14->addItems(ant);
    ui->comboBox_15->addItems(ant);
    ui->comboBox_12->setCurrentText("天线1");
    ui->comboBox_13->setCurrentText("天线2");
    ui->comboBox_14->setCurrentText("天线3");
    ui->comboBox_15->setCurrentText("天线4");

    ui->lineEdit_OneInvTimes->setText("1");

    m_freqregion_radios = new QButtonGroup(this);
    m_freqregion_radios->addButton(ui->rbBtn_FCC, 0);
    m_freqregion_radios->addButton(ui->rbBtn_ETSI, 1);
    m_freqregion_radios->addButton(ui->rbBtn_CHN, 2);
    ui->rbBtn_FCC->setChecked(false);
    connect(m_freqregion_radios, SIGNAL(buttonClicked(int)), this, SLOT(On_Freq_Radios_Clicked(int)) );


    ui->gpBox_CustomFreq->setEnabled(false);

}

void UHF_RFID_Reader::InitWidget()
{
    // tableWidget 列宽初始化


    QStringList labels;
    labels <<"ID"<<"EPC"<<"PC"<<"识别次数"<<"RSSI(dBm)"<<"波特率(MHz)";
    ui->tableWidget_1->setColumnCount(6);
    ui->tableWidget_1->setHorizontalHeaderLabels(labels);
    ui->tableWidget_1->setColumnWidth(0, 60);
    ui->tableWidget_1->setColumnWidth(1, 400);
    ui->tableWidget_1->setColumnWidth(2, 60);
    ui->tableWidget_1->setColumnWidth(3, 160);
    ui->tableWidget_1->setColumnWidth(4, 80);
    ui->tableWidget_1->horizontalHeader()->setStretchLastSection(true);

    QHeaderView* headerView1 = ui->tableWidget_1->verticalHeader();
    headerView1->setHidden(true);
    ui->tableWidget_1->setEditTriggers(QAbstractItemView::NoEditTriggers);


    ui->tableWidget_2->setHorizontalHeaderLabels(labels);
    ui->tableWidget_2->setColumnWidth(0, 60);
    ui->tableWidget_2->setColumnWidth(1, 400);
    ui->tableWidget_2->setColumnWidth(2, 60);
    ui->tableWidget_2->setColumnWidth(3, 160);
    ui->tableWidget_2->setColumnWidth(4, 80);
    QHeaderView* headerView2 = ui->tableWidget_2->verticalHeader();
    headerView2->setHidden(true);
    ui->tableWidget_2->setEditTriggers(QAbstractItemView::NoEditTriggers);

    labels.clear();
    labels <<"序号"<<"PC"<<"CRC"<<"EPC"<<"数据"<<"数据长度"<<"天线"<<"操作次数";
    ui->tableWidget_3->setHorizontalHeaderLabels(labels);
    ui->tableWidget_3->setColumnWidth(0, 60);
    ui->tableWidget_3->setColumnWidth(1, 60);
    ui->tableWidget_3->setColumnWidth(2, 60);
    ui->tableWidget_3->setColumnWidth(3, 250);
    ui->tableWidget_3->setColumnWidth(4, 300);
    ui->tableWidget_3->setColumnWidth(5, 60);
    ui->tableWidget_3->setColumnWidth(6, 40);
    QHeaderView* headerView3 = ui->tableWidget_3->verticalHeader();
    headerView3->setHidden(true);
    ui->tableWidget_3->setEditTriggers(QAbstractItemView::NoEditTriggers);

    // 初始化LCDNumber
    QString ss = QString::asprintf("%05d", 0);
    //ui->lcdNumber1->display(ss);
    //  ui->lcdNumber6->display(ss);

    ss = QString::asprintf("%06d", 0);
    //  ui->lcdNumber2->display(ss);
    //  ui->lcdNumber3->display(ss);
    //ui->lcdNumber7->display(ss);
    //  ui->lcdNumber8->display(ss);

    ss = QString::asprintf("%010d", 0);
    //  ui->lcdNumber4->display(ss);
    //  ui->lcdNumber5->display(ss);
    // ui->lcdNumber9->display(ss);
    // ui->lcdNumber10->display(ss);
}

void UHF_RFID_Reader::On_Connect_Radios_Clicked(int id)
{
    if (id == 0)
    {
        ui->grpBox_Rs->setEnabled(true);
        ui->grpBox_Tcp->setEnabled(false);
        ui->btnConnect_RS->setEnabled(true);
        ui->btnDisconnect_RS->setEnabled(false);
    }
    else
    {
        ui->grpBox_Rs->setEnabled(false);
        ui->grpBox_Tcp->setEnabled(true);
        ui->btnConnect_Tcp->setEnabled(true);
        ui->btnDisconnect_Tcp->setEnabled(false);
    }
}

void UHF_RFID_Reader::On_Drm_Radios_Clicked(int id)
{
    if ( id == 0 )
    {
        m_btDrmMode = 0x01;
    }
    else
    {
        m_btDrmMode = 0x00;
    }
}

void UHF_RFID_Reader::On_Freq_Radios_Clicked(int id)
{
    if ( id == 0 )
    {
        freqregion = 0x01;
        ui->rbBtn_FCC->setChecked(true);
        ui->rbBtn_ETSI->setChecked(false);
        ui->rbBtn_CHN->setChecked(false);
        ui->cbBox_freqStart->clear();
        ui->cbBox_freqEnd->clear();
        QStringList fonts;
        fonts << "902.00" << "902.50" << "903.00" << "903.50" << "904.00" << "904.50" << "905.00" <<
                 "905.50" << "906.00" << "906.50" << "907.00" << "907.50" << "908.00" << "908.50" <<
                 "909.00" << "909.50" << "910.00" << "910.50" << "911.00" << "911.50" << "912.00" <<
                 "912.50" << "913.00" << "913.50" << "914.00" << "914.50" << "915.00" << "915.50" <<
                 "916.00" << "916.50" << "917.00" << "917.50" << "918.00" << "918.50" << "919.00" <<
                 "919.50" << "920.00" << "920.50" << "921.00" << "921.50" << "922.00" << "922.50" <<
                 "923.00" << "923.50" << "924.00" << "924.50" << "925.00" << "925.50" << "926.00" <<
                 "926.50" << "927.00" << "927.50" << "928.00";
        ui->cbBox_freqStart->addItems(fonts);
        ui->cbBox_freqEnd->addItems(fonts);
        ui->cbBox_freqStart->setCurrentText(" ");
        ui->cbBox_freqEnd->setCurrentText(" ");

    }
    else if ( id == 1 )
    {
        freqregion = 0x02;
        ui->rbBtn_FCC->setChecked(false);
        ui->rbBtn_ETSI->setChecked(true);
        ui->rbBtn_CHN->setChecked(false);
        ui->cbBox_freqStart->clear();
        ui->cbBox_freqEnd->clear();
        QStringList fonts;
        fonts << "865.00" << "865.50" << "866.00" << "866.50" << "867.00" << "867.50" << "868.00";
        ui->cbBox_freqStart->addItems(fonts);
        ui->cbBox_freqEnd->addItems(fonts);
        ui->cbBox_freqStart->setCurrentText(" ");
        ui->cbBox_freqEnd->setCurrentText(" ");
    }
    else
    {
        freqregion = 0x03;
        ui->rbBtn_FCC->setChecked(false);
        ui->rbBtn_ETSI->setChecked(false);
        ui->rbBtn_CHN->setChecked(true);
        ui->cbBox_freqStart->clear();
        ui->cbBox_freqEnd->clear();
        QStringList fonts;
        fonts << "920.00" << "920.50" << "921.00" << "921.50" << "922.00" << "922.50" <<
                 "923.00" << "923.50" << "924.00" << "924.50" << "925.00";
        ui->cbBox_freqStart->addItems(fonts);
        ui->cbBox_freqEnd->addItems(fonts);
        ui->cbBox_freqStart->setCurrentText(" ");
        ui->cbBox_freqEnd->setCurrentText(" ");
    }
}

void UHF_RFID_Reader::On_RfLink_Radios_Clicked(int id)
{
    if (id == 0)
    {
        m_profileId = 0xD0;
        ui->rbl_0->setChecked(true);
    }
    else if ( id == 1)
    {
        m_profileId = 0xD1;
        ui->rbl_1->setChecked(true);
    }
    else if( id == 2 )
    {
        m_profileId = 0xD2;
        ui->rbl_2->setChecked(true);
    }
    else if( id == 3 )
    {
        m_profileId = 0xD3;
        ui->rbl_3->setChecked(true);
    }
    else
    {
        return;
    }
}

void UHF_RFID_Reader::On_ReadTag_Radios_Clicked(int id)
{
    if(id == 0)
    {
        m_memBank = 0x00;
    }
    else if( id == 1)
    {
        m_memBank = 0x01;
    }
    else if( id == 2)
    {
        m_memBank = 0x02;
    }
    else
    {
        m_memBank = 0x03;
    }
}


void UHF_RFID_Reader::On_Beeper_Radios_Clicked(int id)
{
    if( 0 == id )
    {
        m_beeper = 0x00;
    }
    else if( 1 == id )
    {
        m_beeper = 0x01;
    }
    else if( 2 == id )
    {
        m_beeper = 0x02;
    }
    else
    {

    }
}

void UHF_RFID_Reader::On_lockMembank_Radios_Clicked(int id)
{
    if( 0 == id ){
        m_memBankLock = 0x00;
    }else if( 1 == id ){
        m_memBankLock = 0x01;
    }else if( 2 == id ){
        m_memBankLock = 0x02;
    }else if( 3 == id ){
        m_memBankLock = 0x03;
    }else{
        m_memBankLock = 0x04;
    }
}
void UHF_RFID_Reader::On_locktype_Radios_Clicked(int id)
{
    if( 0 == id ){
        m_lockType = 0x00;
    }else if( 1 == id ){
        m_lockType = 0x01;
    }else if( 2 == id ){
        m_lockType = 0x02;
    }else{
        m_lockType = 0x03;
    }
}

void UHF_RFID_Reader::On_gpio1_Radios_Clicked(int id){
    if( 0 == id )
    {
        ui->gpio1_high->setChecked(true);
        ui->gpio1_low->setChecked(false);
    }
    else
    {
        ui->gpio1_high->setChecked(false);
        ui->gpio1_low->setChecked(true);
    }
}

void UHF_RFID_Reader::On_gpio2_Radios_Clicked(int id){
    if( 0 == id )
    {
        ui->gpio2_high->setChecked(true);
        ui->gpio2_low->setChecked(false);
    }
    else
    {
        ui->gpio2_high->setChecked(false);
        ui->gpio2_low->setChecked(true);
    }
}
void UHF_RFID_Reader::On_gpio3_Radios_Clicked(int id){
    if( 0 == id )
    {
        ui->gpio3_high->setChecked(true);
        ui->gpio3_low->setChecked(false);
    }
    else
    {
        ui->gpio3_high->setChecked(false);
        ui->gpio3_low->setChecked(true);
    }
}
void UHF_RFID_Reader::On_gpio4_Radios_Clicked(int id){
    if( 0 == id )
    {
        ui->gpio4_high->setChecked(true);
        ui->gpio4_low->setChecked(false);
    }
    else
    {
        ui->gpio4_high->setChecked(false);
        ui->gpio4_low->setChecked(true);
    }
}

void UHF_RFID_Reader::timerEvent(QTimerEvent *event)
{

}

void UHF_RFID_Reader::LockInterface()
{
    ui->gpBox_WorkTemp->setEnabled(false);
    ui->gpBox_Version->setEnabled(false);
    ui->gpBox_Reader->setEnabled(false);
    ui->gpBox_Beeper->setEnabled(false);
    ui->gpBox_Drm->setEnabled(false);
    ui->gpBox_WorkAnt->setEnabled(false);
    ui->gpBox_output->setEnabled(false);
    ui->gpBox_RS->setEnabled(false);
    ui->gpBox_antl->setEnabled(false);
    ui->gpBox_Freq->setEnabled(false);
    ui->gpBox_Xin->setEnabled(false);
    ui->frame->setEnabled(false);

    ui->gpBox_Dataon->setEnabled(false);
    ui->frame_2->setEnabled(false);
    ui->gpBox_Data->setEnabled(false);
    ui->gpBox_chooseLable->setEnabled(false);
    ui->gpBox_readlabel->setEnabled(false);
    ui->gpBox_lock->setEnabled(false);
    ui->gpBox_dslabel->setEnabled(false);
    ui->btnWriteLalel->setEnabled(false);
    ui->gpBox_GPIO->setEnabled(false);
}

void UHF_RFID_Reader::UnlockInterface()
{
    ui->gpBox_WorkTemp->setEnabled(true);
    ui->gpBox_Version->setEnabled(true);
    ui->gpBox_Reader->setEnabled(true);
    ui->gpBox_Beeper->setEnabled(true);
    ui->gpBox_Drm->setEnabled(true);
    ui->gpBox_WorkAnt->setEnabled(true);
    ui->gpBox_output->setEnabled(true);
    ui->gpBox_RS->setEnabled(true);
    ui->gpBox_antl->setEnabled(true);
    ui->gpBox_Freq->setEnabled(true);
    ui->gpBox_Xin->setEnabled(true);
    ui->frame->setEnabled(true);
    ui->gpBox_Dataon->setEnabled(true);
    ui->frame_2->setEnabled(true);
    ui->gpBox_Data->setEnabled(true);
    ui->gpBox_chooseLable->setEnabled(true);
    ui->gpBox_readlabel->setEnabled(true);
    ui->gpBox_lock->setEnabled(true);
    ui->gpBox_dslabel->setEnabled(true);
    ui->btnWriteLalel->setEnabled(true);
    ui->gpBox_GPIO->setEnabled(true);
}

void UHF_RFID_Reader::StopInventory()
{
    ui->btnInventory->setEnabled(true);
    m_isInventory = false;
}

void UHF_RFID_Reader::OnConnected_Tcp()
{
    ui->btnConnect_Tcp->setEnabled(false);
    ui->btnDisconnect_Tcp->setEnabled(true);

}

void UHF_RFID_Reader::ReadMessage()
{
    // qDebug() << "ReadMessage ---------------";
    if(c_pDealData->m_dealIsOver == false)
    {
        // qDebug() << "ReadMessage: is not deal over!";
        return;
    }

    unsigned char buf[MAX_RECV_SOCKET_BUFFER] = {0};
    int recvLen = 0;
    recvLen = m_TcpSocket->read((char*)buf, MAX_RECV_SOCKET_BUFFER-1);
    /* QByteArray recvByte = m_TcpSocket->readAll();
    qDebug("%d", recvByte.length());
    recvLen = recvByte.length();
    memcpy(buf, recvByte.toStdString().c_str(), recvLen);*/
    if (recvLen > 0)
    {
        //处理数据
        // qDebug() << "--------------";
        //CopyCommArg();
        //g_mutex.lock();
        c_pDealData->DecodeThenDealMessage(recvLen, buf, g_msgList);
        m_alysisMsg->Analysis(g_msgList);
        //g_mutex.unlock();
    }
    else
    {
        /*
        QMessageBox box(QMessageBox::Warning, "错误", "接收的数据有问题! ");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();*/
        return;
    }
}





void UHF_RFID_Reader::on_btnConnect_Tcp_clicked()
{
    if(ui->lineEdit_IpAddress->text() == "" || ui->lineEdit_ServerPort->text() == "")
    {
        QMessageBox box(QMessageBox::Warning, "错误", "服务器IP或端口号不能为空!");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, QString("确认"));
        box.exec();
        return;
    }
    /*
    m_TcpSocket->abort(); //取消已有的连接
    m_TcpSocket->connectToHost(ui->lineEdit_IpAddress->text(),
                               ui->lineEdit_ServerPort->text().toInt());
                               */

    QString str = ui->lineEdit_IpAddress->text();
    char* ch;
    QByteArray ba=str.toLatin1();
    ch = ba.data();
    //m_readerMethod->ConnectServer(ch,ui->lineEdit_ServerPort->text().toInt());
    //连接服务器
    m_TcpSocket->abort();
    m_TcpSocket->connectToHost(ch, ui->lineEdit_ServerPort->text().toInt());

    QByteArray buffer = m_TcpSocket->readAll();
    WaitSec(10);
    connect( m_TcpSocket, SIGNAL(readyRead()), this, SLOT(ReadMessage()) );
    // m_TcpSocket = m_readerMethod->GetSocket();

}

void UHF_RFID_Reader::on_btnDisconnect_Tcp_clicked()
{
    m_TcpSocket->disconnectFromHost();
    ui->btnConnect_Tcp->setEnabled(true);
    ui->btnDisconnect_Tcp->setEnabled(false);
    m_Connect = 0;
    this->LockInterface();
    m_btcpInit = false;
}

void UHF_RFID_Reader::on_btn_ReadTemp_clicked()
{

    string str = m_readerMethod->GetReaderTemperature(g_btReadId);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::on_btnReset_clicked()
{

    string str = m_readerMethod->Reset(g_btReadId);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::on_btn_ReadVersion_clicked()
{

    string str = m_readerMethod->GetFirmwareVersion(g_btReadId);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}




void UHF_RFID_Reader::CopyCommArg()
{
    m_commArg.m_Connect = m_Connect;
    m_commArg.m_pTcpSocket = m_TcpSocket;
}

void UHF_RFID_Reader::WaitSec(int sec)
{
    QTime t;
    t.start();
    while(t.elapsed()<1*sec)
        QCoreApplication::processEvents();
}
//epc是否已经检测
int UHF_RFID_Reader::findEpcWidget(string strepc, int& num, int flag)
{
    if(flag == 0)
    {
        int n = ui->tableWidget_1->rowCount();
        for(int i = 0; i < n; i++)
        {
            string curepc = ui->tableWidget_1->item(i, 1)->text().toStdString();
            if(curepc == strepc)
            {
                num = ui->tableWidget_1->item(i,3)->text().toInt();
                return i;
            }
        }
    }
    else if(flag == 1)
    {
        int n = ui->tableWidget_2->rowCount();
        for(int i = 0; i < n; i++)
        {
            string curepc = ui->tableWidget_2->item(i, 1)->text().toStdString();
            if(curepc == strepc)
            {
                num = ui->tableWidget_2->item(i,3)->text().toInt();
                return i;
            }
        }
    }
    else if(flag == 2)
    {
        int n = ui->tableWidget_3->rowCount();
        for(int i = 0; i < n; i++)
        {
            string curepc = ui->tableWidget_3->item(i, 3)->text().toStdString();
            // num = ui->tableWidget_3->item(i,3)->text().toInt();
            if(curepc == strepc)
            {
                return i;
            }
        }
    }
    return -1;
}


void UHF_RFID_Reader::ShowLineEditVersion(char* version, int len)
{
    QString str;
    str = QString(version);
    ui->lineEdit_Version->setText(str);
}

void UHF_RFID_Reader::ShowLineEditTemp(char* pTemp, int len)
{
    QString str(pTemp);
    ui->lineEdit_WorkTemp->setText(str);
}

void UHF_RFID_Reader::ShowLineEditIdentifier(QString identifier)
{
    ui->lineEdit_ReaderIdentifier->setText(identifier);
}

void UHF_RFID_Reader::ShowLineEditOutputPower(int outputPower)
{
    QString str = QString::number(outputPower);
    ui->lineEdit_OutputPower->setText(str);
}

void UHF_RFID_Reader::ShowInventory(InventoryCount inventoryData)
{
    if( 0 == (int)inventoryData.readRate )
    {
        return;
    }
    //ui->lcdNum_TagCount->display((int)inventoryData.tagCount);
    int realtime = (inventoryData.totalRead * 1000) / (int)inventoryData.readRate;
    m_lTotalTime1 += realtime;
    m_nTotalRead1 += inventoryData.totalRead;
    ui->lcdNum_TagCount->display(m_nTagNum1);
    ui->lcdNum_ReadRate->display((int)inventoryData.readRate);
    ui->lcdNum_ReadTime->display(realtime);
    ui->lcdNum_Back->display(m_nBackNum1);\
    ui->lcdNum_totalTime->display(m_lTotalTime1);
}

void UHF_RFID_Reader::ShowReadTagData(TagReadBack tagData)
{
    int num = 0;
    int rowcount = ui->tableWidget_3->rowCount();
    char str[512] = {0};
    memcpy(str, tagData.epc, tagData.epcLen);

    m_nTagNum3 = rowcount+1;
    char id[7] = {0};
    sprintf( id, "%d", m_nTagNum3);

    //  qDebug("chuanru %02X", tagData.pc[0]);
    int workAnt = (int)tagData.workAnt+1;
    char pc[7] = {0};
    char crc[7] = {0};
    sprintf(pc, "%02X %02X ", tagData.pc[0], tagData.pc[1]);
    //   qDebug() << pc;
    int i;
    char epc[64] = {0};
    sprintf(crc, "%02X %02X ", tagData.crc[0], tagData.crc[1]);

    char *pe = epc;

    for(i = 0; i < tagData.epcLen; i++)
    {
        sprintf(pe, "%02X ", tagData.epc[i]);
        pe += 3;
    }

    //  qDebug() << "error";
    //   qDebug() << pc;

    int readLen = (int)tagData.readLen;
    int readCount = (int)tagData.readCount;
    char readData[64] = {0};
    char* p = readData;
    for(i = 0; i < tagData.readLen; i++)
    {
        sprintf(p, "%02X ", tagData.readData[i]);
        p += 3;
    }
    string epcstr(epc);
    int n = this->findEpcWidget(epcstr, num, 2);
    if(n < 0)
    {

        ui->tableWidget_3->insertRow(rowcount);
        ui->tableWidget_3->setItem( rowcount, 0, new QTableWidgetItem(QString(id)) );
        ui->tableWidget_3->setItem( rowcount, 1, new QTableWidgetItem(QString((const char*)pc)) );
        ui->tableWidget_3->setItem( rowcount, 2, new QTableWidgetItem(QString((const char*)crc)) );
        ui->tableWidget_3->setItem( rowcount, 3, new QTableWidgetItem(QString((const char*)epc)) );
        ui->tableWidget_3->setItem( rowcount, 4, new QTableWidgetItem(QString((const char*)readData)) );
        ui->tableWidget_3->setItem( rowcount, 5, new QTableWidgetItem(QString::number(readLen)) );
        ui->tableWidget_3->setItem( rowcount, 6, new QTableWidgetItem(QString::number(workAnt)) );
        ui->tableWidget_3->setItem( rowcount, 7, new QTableWidgetItem(QString::number(readCount)) );


    }
    else
    {
        return;
    }
}

void UHF_RFID_Reader::ShowEpcBuffer(EpcData epcData)
{
    m_nBackNum1 += 1;
    MapEpcData mapepc;
    memset(&mapepc, 0, sizeof(MapEpcData));

    int rowcount = ui->tableWidget_1->rowCount();
    m_nTagNum1 = rowcount;
    mapepc.id = m_nTagNum1;
    mapepc.epcLen = epcData.epcLen;
    mapepc.freqPoint = epcData.freqPoint;
    mapepc.rssi = epcData.rssi;
    memcpy( mapepc.pc, epcData.pc, 2 );
    memcpy( mapepc.epc, epcData.epc, epcData.epcLen );

    int i;
    char pc[7] = {0};
    if(mapepc.epcLen > 1024)
    {
        return;
    }

    char *epcStr = new char[mapepc.epcLen*3+1];
    char *p = epcStr;
    memset(epcStr, 0, mapepc.epcLen*3+1);
    for(i=0; i<epcData.epcLen; i++)
    {
        sprintf(p, "%02X ", (char*)mapepc.epc[i]);
        p += 3;
    }
    sprintf(pc, "%02X %02X", (char*)mapepc.pc[0], (char*)mapepc.pc[1]);
    char id[7] = {0};
    sprintf(id, "%d", m_nTagNum1);


    // ui->tableWidget_1->insertRow( m_nTagNum );
    /*
    ui->tableWidget_1->insertRow(rowcount);
    ui->tableWidget_1->setItem( rowcount, 0, new QTableWidgetItem(QString(id)) );
    ui->tableWidget_1->setItem( rowcount, 1, new QTableWidgetItem(QString(epcStr)) );
    ui->tableWidget_1->setItem( rowcount, 2, new QTableWidgetItem(QString(pc)) );
    ui->tableWidget_1->setItem( rowcount, 3, new QTableWidgetItem(QString("1")) ); //识别次数
    ui->tableWidget_1->setItem( rowcount, 4, new QTableWidgetItem(QString((int)mapepc.rssi)) );
    ui->tableWidget_1->setItem( rowcount, 5, new QTableWidgetItem(QString((int)mapepc.freqPoint)) );
*/
    //qDebug() << mapepc.id << QString(epcStr);
    // delete epcStr;
    //ui->tableWidget_1->setItem();

    // for(map<string, Struct_ListEpcText>::iterator it = g_EpcText.begin(); it != g_EpcText.end(); it++)
    //   {
    //  QMutex mutex;
    //  mutex.lock();
    map<unsigned char, string>::iterator itRssi, itFreq;
    itRssi = m_mapRssi.find(mapepc.rssi);
    itFreq = m_mapFreq.find(mapepc.freqPoint);
    if(itRssi == m_mapRssi.end())
    {
        // qDebug() << "not find rssi";
    }
    if(itFreq == m_mapFreq.end())
    {
        //  qDebug() << "not find freq";
    }

    int nEpcTimes = 0;
    string strepc(epcStr);
    int n = this->findEpcWidget(strepc, nEpcTimes, 0);
    mapepc.totalTimes = nEpcTimes + 1;
    if( n < 0 )
    {
        //在已显示的项中，没有找到要显示的，则添加
        ui->tableWidget_1->insertRow(rowcount);
        ui->tableWidget_1->setItem( rowcount, 0, new QTableWidgetItem(QString(id)) );
        ui->tableWidget_1->setItem( rowcount, 1, new QTableWidgetItem(QString(epcStr)) );
        ui->tableWidget_1->setItem( rowcount, 2, new QTableWidgetItem(QString(pc)) );
        ui->tableWidget_1->setItem( rowcount, 3, new QTableWidgetItem(QString::number(mapepc.totalTimes)) ); //识别次数
        if (itRssi != m_mapRssi.end())
        {
            QString s = QString::fromStdString(itRssi->second);
            ui->tableWidget_1->setItem( rowcount, 4, new QTableWidgetItem(s));
        }/*
            else {
                ui->tableWidget_1->setItem( rowcount, 4, new QTableWidgetItem(QString((int)mapepc.rssi)) );
            }*/
        if ( itFreq != m_mapFreq.end() ) {
            QString s = QString::fromStdString(itFreq->second);
            ui->tableWidget_1->setItem( rowcount, 5, new QTableWidgetItem(s) );
        }
        /*
            else{
                ui->tableWidget_1->setItem( rowcount, 5, new QTableWidgetItem(QString((int)mapepc.freqPoint)) );
            }*/
    }
    else
    {

        //找到，则修改
        ui->tableWidget_1->item(n, 3)->setText(QString::number(mapepc.totalTimes) );

        if (itRssi != m_mapRssi.end())
        {
            QString qstr = "";
            string s = itRssi->second;
            qstr = qstr + QString(s.c_str());
            ui->tableWidget_1->item( n, 4)->setText(qstr);
        }
        /*
            else {
                ui->tableWidget_1->item( n, 4)->setText(QString((int)mapepc.rssi));
            }*/
        if ( itFreq != m_mapFreq.end() ) {
            /*
                QString s = QString(itFreq->second.c_str());
                s.append("MHz");
                ui->tableWidget_1->item( n, 5)->setText(s);
                qDebug( "freq : %s", itFreq->second.c_str());
                */
            string s = itFreq->second;
            QString freq = QString(s.c_str());
            freq.append("MHz");
            ui->tableWidget_1->item( n, 5)->setText(freq);
        }
        /* 错误频率，不处理
            else{
                ui->tableWidget_1->item( n, 5)->setText(QString((int)mapepc.freqPoint));
                qDebug() << "error freq :";
            }*/

    }


    if(ui->minDBM->text().isEmpty() && ui->maxDBM->text().isEmpty()) {
        ui->minDBM->setText(QString(itRssi->second.c_str()));
        ui->maxDBM->setText(QString(itRssi->second.c_str()));
    }
    else
    {
        //int rssi = -9;
        int rssi = atoi( itRssi->second.c_str() );
        if(rssi < ui->minDBM->text().toInt())
        {
            ui->minDBM->setText(QString(itRssi->second.c_str()));
        }
        if(rssi > ui->maxDBM->text().toInt())
        {
            ui->maxDBM->setText(QString(itRssi->second.c_str()));
        }
    }
    if( -1 == m_labelList.indexOf(QString(epcStr)) )
    {
        m_labelList << QString(epcStr);
        ui->cbBox_labelList->addItem(QString(epcStr));
    }

    // mutex.unlock();
    delete[] epcStr;
}

void UHF_RFID_Reader::ShowFastInventory(int labeltime, int commandtime)
{
    if( 0 == commandtime )
    {
        return;
    }
    // qDebug() << "ShowFastInventory ";
    int realtime = commandtime;
    int rate = labeltime * 1000 / commandtime;
    m_lTotalTime2 += realtime;
    m_nTotalRead2 += labeltime;
    ui->lcdNum_TagCount1->display(m_nTagNum2);
    ui->lcdNum_ReadRate1->display(rate);
    ui->lcdNum_ReadTime1->display(realtime);
    ui->lcdNum_Back1->display(m_nBackNum2);\
    ui->lcdNum_totalTime1->display(m_lTotalTime2);
}

void UHF_RFID_Reader::ShowFastEpcBuffer(EpcData epcData)
{
    //  qDebug() << "ShowFastEpcBuffer ";
    m_nBackNum2 += 1;
    MapEpcData mapepc;
    memset(&mapepc, 0, sizeof(MapEpcData));

    int rowcount = ui->tableWidget_2->rowCount();
    m_nTagNum2 = rowcount;
    mapepc.id = m_nTagNum2;
    mapepc.epcLen = epcData.epcLen;
    mapepc.freqPoint = epcData.freqPoint;
    if( mapepc.epcLen > EPC_MAX_LEN )
    {
        return;
    }

    mapepc.rssi = epcData.rssi;
    memcpy( mapepc.pc, epcData.pc, 2 );
    memcpy( mapepc.epc, epcData.epc, epcData.epcLen );

    int i;
    char pc[7] = {0};
    char *epcStr = new char[mapepc.epcLen*3+1];
    memset(epcStr, 0, mapepc.epcLen*3+1);
    char *p = epcStr;

    for(i=0; i<epcData.epcLen; i++)
    {
        sprintf(p, "%02X ", (char*)mapepc.epc[i]);
        p += 3;
    }
    sprintf(pc, "%02X %02X", (char*)mapepc.pc[0], (char*)mapepc.pc[1]);
    char id[7] = {0};
    sprintf(id, "%d", m_nTagNum2);

    //  QMutex mutex;
    // mutex.lock();

    map<unsigned char, string>::iterator itRssi, itFreq;
    itRssi = m_mapRssi.find(mapepc.rssi);
    itFreq = m_mapFreq.find(mapepc.freqPoint);

    //  qDebug() << itFreq->second.c_str();
    /*
    if(itRssi == m_mapRssi.end())
    {
        qDebug() << "not find rssi";
    }
    if(itFreq == m_mapFreq.end())
    {
        qDebug() << "not find freq";
    }*/

    int nEpcTimes = 0;
    string strepc(epcStr);
    int n = this->findEpcWidget(strepc, nEpcTimes, 1);
    mapepc.totalTimes = nEpcTimes + 1;
    if( n < 0 )
    {
        //在已显示的项中，没有找到要显示的，则添加
        ui->tableWidget_2->insertRow(rowcount);
        ui->tableWidget_2->setItem( rowcount, 0, new QTableWidgetItem(QString(id)) );
        ui->tableWidget_2->setItem( rowcount, 1, new QTableWidgetItem(QString(epcStr)) );
        ui->tableWidget_2->setItem( rowcount, 2, new QTableWidgetItem(QString(pc)) );
        ui->tableWidget_2->setItem( rowcount, 3, new QTableWidgetItem(QString::number(mapepc.totalTimes)) ); //识别次数
        if (itRssi == m_mapRssi.end() || itFreq == m_mapFreq.end()) {
            ui->tableWidget_2->setItem( rowcount, 4, new QTableWidgetItem(QString((int)mapepc.rssi)) );
            ui->tableWidget_2->setItem( rowcount, 5, new QTableWidgetItem(QString((int)mapepc.freqPoint)) );
        }

        else {
            ui->tableWidget_2->setItem( rowcount, 4, new QTableWidgetItem(QString(itRssi->second.c_str())) );
            ui->tableWidget_2->setItem( rowcount, 5, new QTableWidgetItem(QString(itFreq->second.c_str())) );
        }

    }
    else
    {
        //找到，则修改
        ui->tableWidget_2->item(n, 3)->setText(QString::number(mapepc.totalTimes) );

        if (itRssi != m_mapRssi.end())
        {
            QString qstr;
            string s = itRssi->second;
            qstr += QString(s.c_str());
            if( !qstr.isEmpty() )
            {
                ui->tableWidget_2->item( n, 4)->setText(qstr);
            }


        }/*
            else {
                ui->tableWidget_2->item( n, 4)->setText(QString((int)mapepc.rssi));
            }*/
        if ( itFreq != m_mapFreq.end() ) {
            /*QString s = QString(itFreq->second.c_str());
                if( !s.isEmpty() )
                {
                    s.append("MHz");
                    ui->tableWidget_2->item( n, 5)->setText(s);
                }
                */
            string s = itFreq->second;
            QString freq = QString(s.c_str());
            freq.append("MHz");
            ui->tableWidget_2->item( n, 5)->setText(freq);
        }/*
            else{
                ui->tableWidget_2->item( n, 5)->setText(QString((int)mapepc.freqPoint));
            }*/
    }

    if(ui->minDBM1->text().isEmpty() && ui->maxDBM1->text().isEmpty()) {
        ui->minDBM1->setText(QString(itRssi->second.c_str()));
        ui->maxDBM1->setText(QString(itRssi->second.c_str()));
    }
    else
    {
        int rssi = atoi( itRssi->second.c_str() );
        if(rssi < ui->minDBM1->text().toInt())
        {
            ui->minDBM1->setText(QString(itRssi->second.c_str()));
        }
        if(rssi > ui->maxDBM1->text().toInt())
        {
            ui->maxDBM1->setText(QString(itRssi->second.c_str()));
        }
    }
    //   }
    if( -1 == m_labelList.indexOf(QString(epcStr)) )
    {
        QString epclabel = QString(epcStr);
        m_labelList << epclabel;
        ui->cbBox_labelList->addItem(epclabel);
    }

    //mutex.unlock();
    WaitSec(1);
    delete[] epcStr;
}

void UHF_RFID_Reader::DrmModeGet(unsigned char drmMode)
{
    if(drmMode == 0x00)
    {
        ui->rbBtn_DrmOpen->setChecked(false);
        ui->rbBtn_DrmClose->setChecked(true);
        m_btDrmMode = drmMode;
    }
    else if(drmMode == 0x01)
    {
        ui->rbBtn_DrmOpen->setChecked(true);
        ui->rbBtn_DrmClose->setChecked(false);
        m_btDrmMode = drmMode;
    }
    else
    {
        //获取DRM失败，请重试
    }
}

void UHF_RFID_Reader::ReadWorkAnt(unsigned char workAnt)
{
    if( workAnt == 0x00 )
    {
        ui->cmbBox_ant->setCurrentText(QString("天线 1"));
    }
    else if ( workAnt == 0x01 )
    {
        ui->cmbBox_ant->setCurrentText(QString("天线 2"));
    }
    else if ( workAnt == 0x02 )
    {
        ui->cmbBox_ant->setCurrentText(QString("天线 3"));
    }
    else if ( workAnt == 0x03 )
    {
        ui->cmbBox_ant->setCurrentText(QString("天线 4"));
    }
    else
    {
        return;
    }
}

void UHF_RFID_Reader::AntDetector(unsigned char antState)
{
    if ( antState == 0x00 )
    {
        //表示关闭天线连接检测

    }
    else
    {

    }
    ui->lineEdit_antDetector->setText(QString::number((int)antState));
}

void UHF_RFID_Reader::ReturnLoss(unsigned char returnLoss)
{
    ui->lineEdit_RL->setText(QString::number((int)returnLoss));
}
//系统默认频点
void UHF_RFID_Reader::FreqRegion(FreqRegionDefault freq)
{
    ui->ckBox_Custom->setChecked(false);
    ui->gpBox_CustomFreq->setEnabled(false);
    ui->gpBox_freq->setEnabled(true);
    if( freq.region == 0x01 )
    {
        On_Freq_Radios_Clicked(0);
    }
    else if( freq.region == 0x02 )
    {
        On_Freq_Radios_Clicked(1);
    }
    else
    {
        On_Freq_Radios_Clicked(2);
    }
    map<unsigned char, string>::iterator it;
    it = m_mapFreq.find(freq.startFreq);
    if(it != m_mapFreq.end()){
        ui->cbBox_freqStart->setCurrentText( QString::fromStdString(it->second) );
    }
    else{

    }
    it = m_mapFreq.find(freq.endFreq);
    if(it != m_mapFreq.end()){
        ui->cbBox_freqEnd->setCurrentText( QString::fromStdString(it->second) );
    }
    else{

    }
}
//用户自定频点
void UHF_RFID_Reader::CustomFreqRegion(FreqRegionCustom freq, int startFreq)
{
    ui->ckBox_Custom->setChecked(true);
    ui->gpBox_CustomFreq->setEnabled(true);
    ui->gpBox_freq->setEnabled(false);

    ui->lineEdit_freqGap->setText(QString::number((int)freq.freqSpace * 10));
    ui->lineEdit_FreqNum->setText(QString::number((int)freq.freqQuantity));
    ui->lineEdit_customStart->setText(QString::number(startFreq));

}


void UHF_RFID_Reader::RfLinkProfile(unsigned char profileID)
{
    if( 0xD0 == profileID )
    {
        On_RfLink_Radios_Clicked(0);
    }
    else if( 0xD1 == profileID )
    {
        On_RfLink_Radios_Clicked(1);
    }
    else if( 0xD2 == profileID )
    {
        On_RfLink_Radios_Clicked(2);
    }
    else if( 0xD3 == profileID )
    {
        On_RfLink_Radios_Clicked(3);
    }
    else
    {

    }
}

void UHF_RFID_Reader::GetAccessEpc(unsigned char* epc, int epcLen)
{
    unsigned char epcstr[64] = {0};
    memcpy(epcstr, epc, epcLen);
}

void UHF_RFID_Reader::ShowGPIOValue(unsigned char gpio1, unsigned char gpio2)
{
    if( 0x00 == gpio1 )
    {
        On_gpio1_Radios_Clicked(1);
    }
    else
    {
        On_gpio1_Radios_Clicked(0);
    }
    if( 0x00 == gpio2 )
    {
        On_gpio2_Radios_Clicked(1);
    }
    else
    {
        On_gpio2_Radios_Clicked(0);
    }

}

void UHF_RFID_Reader::ShowTxtLog(string strlog)
{
    if( m_logNum > 20 )
    {
        m_logNum = 1;
        ui->txtLog->clear();
    }
    else
    {
        m_logNum++;
    }

    ui->txtLog->append(QString::fromStdString(strlog/*+"\n"*/));
}

void UHF_RFID_Reader::on_btnReadIdentifier_clicked()
{

    string getReadIdentStr = m_readerMethod->GetReaderIdentifier(g_btReadId);
    if(getReadIdentStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)getReadIdentStr.c_str(), getReadIdentStr.length());
}

void UHF_RFID_Reader::on_btn_GetOutputPower_clicked()
{

    string getOutputPowerStr = m_readerMethod->GetOutputPower(g_btReadId);
    if(getOutputPowerStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)getOutputPowerStr.c_str(), getOutputPowerStr.length());
}

void UHF_RFID_Reader::on_btn_SetOutputPower_clicked()
{

    QString str = ui->lineEdit_OutputPower->text();
    if (str.isEmpty() || str.toInt() < 0 || str.toInt() > 33)
    {
        return;
    }
    string setOutputPowerStr = m_readerMethod->SetOutputPower(g_btReadId, (unsigned char)ui->lineEdit_OutputPower->text().toInt());
    //m_readerMethod->SetOutputPower(g_btReadId, x);
    if(setOutputPowerStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)setOutputPowerStr.c_str(), setOutputPowerStr.length());
}

void UHF_RFID_Reader::on_btnGetDrmMode_clicked()
{
    string str = m_readerMethod->GetDrmMode(g_btReadId);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::on_btnSetDrmMode_clicked()
{
    if(m_btDrmMode == 0xFF)
    {
        QMessageBox box(QMessageBox::Warning, "错误", "请选择DRM状态");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    string str = m_readerMethod->SetDrmMode(g_btReadId, m_btDrmMode);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::on_ckBox_Session_clicked()
{
    if(ui->ckBox_Session->checkState() == Qt::Unchecked)
    {
        ui->cmbBox_SessionID->setEnabled(false);
        ui->cmbBox_InventFlag->setEnabled(false);
        ui->label_SessionId->setEnabled(false);
        ui->label_InventoriedFlag->setEnabled(false);
    }
    else
    {
        ui->cmbBox_SessionID->setEnabled(true);
        ui->cmbBox_InventFlag->setEnabled(true);
        ui->label_SessionId->setEnabled(true);
        ui->label_InventoriedFlag->setEnabled(true);
    }
}

void UHF_RFID_Reader::on_btnInventory_clicked()
{
    m_isInventory = true;
    ui->btnInventory->setEnabled(false);
    ui->btnStopInventory->setEnabled(true);
    unsigned char ant[5];
    unsigned char ch = 0xFF;
    memset(&ant, ch, 5);
    if (ui->ckBox_Ant1->checkState() == Qt::Checked) { ant[0] = 0x00; m_Inventory = true; }
    if (ui->ckBox_Ant2->checkState() == Qt::Checked) { ant[1] = 0x01; m_Inventory = true; }
    if (ui->ckBox_Ant3->checkState() == Qt::Checked) { ant[2] = 0x02; m_Inventory = true; }
    if (ui->ckBox_Ant4->checkState() == Qt::Checked) { ant[3] = 0x03; m_Inventory = true; }
    int i = 0;
    for(i = 0; i < 4; ++i)
    {
        if( ant[i] != 0xFF )
            break;
    }
    if( 4 == i )
        m_Inventory = false;

    if (ui->lineEdit_OneInvTimes->text().isEmpty() ||
            ui->lineEdit_OneInvTimes->text().toInt() > 255 ||
            ui->lineEdit_OneInvTimes->text().toInt() < 0 )
    {
        QMessageBox box(QMessageBox::Warning, "错误", "请输入每条命令盘存次数");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        ui->btnInventory->setEnabled(true);
        ui->btnStopInventory->setEnabled(false);
        return;
    }

    if( m_Inventory == false ) //是否设置天线
    {
        QMessageBox box(QMessageBox::Warning, "错误", "请选择工作天线");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        ui->btnInventory->setEnabled(true);
        ui->btnStopInventory->setEnabled(false);
        return;
    }
    else
    {

        //盘存标签
        //  unsigned char btRound = 0x00;
        ui->btnInventory->setCheckable(true);
        //   int nSendState = 0;
        unsigned char btWorkAnt = 0xFF;

        //  if( m_isInventory )
        while (m_isInventory)
        {

            int i = 0, j=0;
            for(i = j; i < 4; i++){
                if(ant[i] == 0xFF)
                {
                    continue;
                }
                else
                {
                    btWorkAnt = ant[i];
                    j = i+1;
                }

                // if (nSendState == 0) {

                /*
                    if(ui->ckBox_Ant1->checkState() == Qt::Checked) {
                        btWorkAnt = 0x00;
                    }*/
                string setWorkAntStr = m_readerMethod->SetWorkAntenna(g_btReadId, btWorkAnt);
                if(setWorkAntStr.length() == 0){
                    return;
                }
                m_TcpSocket->write((const char*)setWorkAntStr.c_str(), setWorkAntStr.length());
                // nSendState = 1;
                // }
                //  else
                // {
                WaitSec(30);
                unsigned char btRound = 0x01;
                if(ui->ckBox_Session->checkState() != Qt::Checked ) {
                    string invRealStr = m_readerMethod->InventoryReal(g_btReadId, btRound);
                    if(invRealStr.length() == 0){
                        return;
                    }
                    m_TcpSocket->write((const char*)invRealStr.c_str(), invRealStr.length());
                }
                else
                {
                    unsigned char session, flag;
                    if( ui->cmbBox_SessionID->currentText() == "A0" )
                    { session = 0x01; }
                    else if( ui->cmbBox_SessionID->currentText() == "A1" )
                    { session = 0x02; }
                    else if( ui->cmbBox_SessionID->currentText() == "A2" )
                    { session = 0x03; }
                    else if( ui->cmbBox_SessionID->currentText() == "A3" )
                    { session = 0x04; }
                    if( ui->cmbBox_InventFlag->currentText() == "A" )
                    { flag = 0x00; }
                    else if( ui->cmbBox_InventFlag->currentText() == "B" )
                    { flag = 0x01; }
                    string customInvStr = m_readerMethod->CustomizedInventory(g_btReadId, session, flag, btRound );
                    if(customInvStr.length() == 0){
                        return;
                    }
                    m_TcpSocket->write((const char*)customInvStr.c_str(), customInvStr.length());
                }

                //   nSendState = 0;
                // }
                WaitSec(10);
            }
        }
    }

}

void UHF_RFID_Reader::on_ckBox_Ant1_clicked()
{
    /*
    if(ui->ckBox_Ant1->checkState() == Qt::Checked)
    {
        m_Inventory = true;
        ui->ckBox_Ant1->setCheckable(Qt::Checked);

        unsigned char btWorkAntenna = 0x00;
        m_readerMethod->SetWorkAntenna(g_btReadId, btWorkAntenna);

    }
    else
    {
        m_Inventory = false;
        m_nWorkAnt = 0;
        //ui->ckBox_Ant1->setCheckState(Qt::Unchecked);
        ui->ckBox_Ant2->setCheckState(Qt::Unchecked);
        ui->ckBox_Ant3->setCheckState(Qt::Unchecked);
        ui->ckBox_Ant4->setCheckState(Qt::Unchecked);
    }
    */
}

void UHF_RFID_Reader::on_ckBox_Ant2_clicked()
{

}

void UHF_RFID_Reader::on_ckBox_Ant3_clicked()
{

}

void UHF_RFID_Reader::on_ckBox_Ant4_clicked()
{

}

void UHF_RFID_Reader::on_radioButton_Beeper1_clicked()
{

}

void UHF_RFID_Reader::on_btnGetWorkAnt_clicked()
{

    string str = m_readerMethod->GetWorkAntenna(g_btReadId);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::GetFastAnt(AntData *antData, int nLen)
{
    if(ui->comboBox_12->currentText() == "天线1")
    {   antData[0].ant = 0x00;}
    else if(ui->comboBox_12->currentText() == "天线2")
    {   antData[0].ant = 0x01;  }
    else if(ui->comboBox_12->currentText() == "天线3")
    {   antData[0].ant = 0x02;  }
    else if(ui->comboBox_12->currentText() == "天线4")
    {   antData[0].ant = 0x03;  }
    else
    {   antData[0].ant = 0xFF;  }
    antData[0].times = (unsigned char)atoi(ui->lineEdit_18->text().toStdString().c_str());

    if(ui->comboBox_13->currentText() == "天线1")
    {   antData[1].ant = 0x00;  }
    else if(ui->comboBox_13->currentText() == "天线2")
    {   antData[1].ant = 0x01;  }
    else if(ui->comboBox_13->currentText() == "天线3")
    {   antData[1].ant = 0x02;  }
    else if(ui->comboBox_13->currentText() == "天线4")
    {   antData[1].ant = 0x03;  }
    else
    {   antData[1].ant = 0xFF;  }
    antData[1].times = (unsigned char)atoi(ui->lineEdit_19->text().toStdString().c_str());

    if(ui->comboBox_14->currentText() == "天线1")
    {   antData[2].ant = 0x00;  }
    else if(ui->comboBox_14->currentText() == "天线2")
    {   antData[2].ant = 0x01;  }
    else if(ui->comboBox_14->currentText() == "天线3")
    {   antData[2].ant = 0x02;  }
    else if(ui->comboBox_14->currentText() == "天线4")
    {   antData[2].ant = 0x03;  }
    else
    {   antData[2].ant = 0xFF;  }
    antData[2].times = (unsigned char)atoi(ui->lineEdit_20->text().toStdString().c_str());

    if(ui->comboBox_15->currentText() == "天线1")
    {   antData[3].ant = 0x00;  }
    else if(ui->comboBox_15->currentText() == "天线2")
    {   antData[3].ant = 0x01;  }
    else if(ui->comboBox_15->currentText() == "天线3")
    {   antData[3].ant = 0x02;  }
    else if(ui->comboBox_15->currentText() == "天线4")
    {   antData[3].ant = 0x03;  }
    else
    {   antData[3].ant = 0xFF;  }
    antData[3].times = (unsigned char)atoi(ui->lineEdit_21->text().toStdString().c_str());
}

char UHF_RFID_Reader::a2x(char ch)
{
    switch(ch)
    {
    case '1':
        return 1;
    case '2':
        return 2;
    case '3':
        return 3;
    case '4':
        return 4;
    case '5':
        return 5;
    case '6':
        return 6;
    case '7':
        return 7;
    case '8':
        return 8;
    case '9':
        return 9;
    case 'A':
    case 'a':
        return 10;
    case 'B':
    case 'b':
        return 11;
    case 'C':
    case 'c':
        return 12;
    case 'D':
    case 'd':
        return 13;
    case 'E':
    case 'e':
        return 14;
    case 'F':
    case 'f':
        return 15;
    default:
        break;;
    }

    return 0;
}

int  UHF_RFID_Reader::HexToChar(char* inbuffer, int iLen, char* cbuf, int& oLen)
{/*
    char *p = inbuffer;
    char* ch = outbuffer;
    int i, j;
    j = 0;
    for(i = 0; i<iLen; i++)
    {
        if(p[i] == 0 && p[i+1] == 0)
        {
            return -1;
        }
        if(p[i] == 0 )
        {
            continue;
        }
        if( p[i] != 0 && p[i+1] != 0 )
        {
            ch[j] = (a2x(p[i])<<4) | a2x(p[i+1]);
            i++;
            j++;
        }
    }
    return 0;*/
    char high, low;
    int idx, ii=0;
    for (idx=0; idx<iLen; idx+=2)
    {
        high = inbuffer[idx];
        low = inbuffer[idx+1];

        if(high>='0' && high<='9')
            high = high-'0';
        else if(high>='A' && high<='F')
            high = high - 'A' + 10;
        else if(high>='a' && high<='f')
            high = high - 'a' + 10;
        else
            return -1;

        if(low>='0' && low<='9')
            low = low-'0';
        else if(low>='A' && low<='F')
            low = low - 'A' + 10;
        else if(low>='a' && low<='f')
            low = low - 'a' + 10;
        else
            return -1;

        cbuf[ii++] = high<<4 | low;
    }
    return 0;
}

void UHF_RFID_Reader::cutStrZero(string& str)
{
    int begin = 0;
    begin = str.find(" ", begin);
    while(begin != -1)
    {
        str.replace(begin, 1, "");
        begin = str.find(" ", begin);
    }
}



void UHF_RFID_Reader::on_btnFastInventory_clicked()
{
    m_isInventoryFast = true;
    ui->btnFastInventory->setEnabled(false);
    ui->btnStopFastInventory->setEnabled(true);
    unsigned char aryData[11];
    AntData antData[4];
    memset(&aryData, 0, 11);
    memset(&antData, 0, 11);
    GetFastAnt(antData, 4);
    int i;
    int n = 0;
    for(i = 0; i < 4; i++){
        aryData[n] = antData[i].ant;
        aryData[n+1] = antData[i].times;
        n += 2;
    }
    aryData[8] = (unsigned char)atoi(ui->lineEdit_22->text().toStdString().c_str());
    aryData[9] = (unsigned char)atoi(ui->lineEdit_23->text().toStdString().c_str());

    if (!m_bFastInventory) {
        return;
    }
    while(m_isInventoryFast)
    {
        string str = m_readerMethod->FastSwitchInventory(g_btReadId, aryData, 10);
        if(str.length() == 0){
            return;
        }
        m_TcpSocket->write((const char*)str.c_str(), str.length());
        WaitSec(100);
    }
}

void UHF_RFID_Reader::on_btnStopInventory_clicked()
{
    ui->btnInventory->setEnabled(true);
    ui->btnStopInventory->setEnabled(false);
    m_isInventory = false;

}

void UHF_RFID_Reader::on_btnStopFastInventory_clicked()
{
    ui->btnFastInventory->setEnabled(true);
    ui->btnStopFastInventory->setEnabled(false);
    m_isInventoryFast = false;
}



void UHF_RFID_Reader::on_btnSetWorkAnt_clicked()
{
    unsigned char workant = 0xFF;
    QString antStr = ui->cmbBox_ant->currentText();
    if ( antStr == "天线 1" )
    {
        workant = 0x00;
    }
    else if( antStr == "天线 2" )
    {
        workant = 0x01;
    }
    else if( antStr == "天线 3" )
    {
        workant = 0x02;
    }
    else if( antStr == "天线 4" )
    {
        workant = 0x03;
    }
    if(workant == 0xFF)
    {
        return;
    }
    string str = m_readerMethod->SetWorkAntenna(g_btReadId, workant);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::on_btnReturnLoss_clicked()
{
    string str = ui->cmbBox_RL->currentText().toStdString();
    map<unsigned char, string>::iterator freq_it;
    for(freq_it = m_mapFreq.begin(); freq_it != m_mapFreq.end(); freq_it++)
    {
        if( str == freq_it->second )
        {
            unsigned char btFreq;
            btFreq = freq_it->first;
            string str = m_readerMethod->MeasureReturnLoss(g_btReadId, btFreq);
            if(str.length() == 0){
                return;
            }
            m_TcpSocket->write((const char*)str.c_str(), str.length());
        }

    }

}

void UHF_RFID_Reader::on_btnAntDetectorGet_clicked()
{
    string str = m_readerMethod->GetAntDetector(g_btReadId);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::on_btnAntDetectorSet_clicked()
{
    unsigned char detectorState = 0xFF;
    if ( ui->lineEdit_antDetector->text().toInt() > 255 )
    {
        QMessageBox box(QMessageBox::Warning, "错误", "请输入255以内的整数！");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    detectorState = (unsigned char)ui->lineEdit_antDetector->text().toInt();
    string str = m_readerMethod->SetAntDetector(g_btReadId, detectorState);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::on_btnGetFreqRegion_clicked()
{
    string str = m_readerMethod->GetFrequencyRegion(g_btReadId);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::on_ckBox_Custom_clicked()
{
    if ( ui->ckBox_Custom->checkState() == Qt::Checked )
    {
        freqregion = 0x04;
        ui->gpBox_freq->setEnabled(false);
        ui->gpBox_CustomFreq->setEnabled(true);
    }
    else
    {
        ui->gpBox_freq->setEnabled(true);
        ui->gpBox_CustomFreq->setEnabled(false);
    }
}

void UHF_RFID_Reader::on_btnSetFreqRegion_clicked()
{
    if ( ui->ckBox_Custom->checkState() == Qt::Checked )
    {
        FreqRegionCustom freq;
        freq.region = freqregion;
        freq.freqSpace = (unsigned char)(ui->lineEdit_freqGap->text().toInt() / 10 );
        freq.freqQuantity = (unsigned char)ui->lineEdit_FreqNum->text().toInt();

        string setUserFreq = m_readerMethod->SetUserDefineFrequency(g_btReadId, ui->lineEdit_customStart->text().toInt(), freq.freqSpace, freq.freqQuantity);
        if(setUserFreq.length() == 0){
            return;
        }
        m_TcpSocket->write((const char*)setUserFreq.c_str(), setUserFreq.length());
    }
    else
    {
        unsigned char startFreq = 0xFF, endFreq = 0xFF;
        map<unsigned char, string>::iterator itstart, itend;
        for(itstart = m_mapFreq.begin(); itstart != m_mapFreq.end(); itstart++)
        {
            if(ui->cbBox_freqStart->currentText().toStdString() == itstart->second)
            {
                startFreq = itstart->first;
                break;
            }
        }
        for(itend = m_mapFreq.begin(); itend != m_mapFreq.end(); itend++)
        {
            if(ui->cbBox_freqEnd->currentText().toStdString() == itend->second)
            {
                endFreq = itend->first;
                break;
            }
        }
        if(itstart == m_mapFreq.end() || itend == m_mapFreq.end())
        {
            QMessageBox box(QMessageBox::Warning, "错误", "请设置正确的频率");
            box.setStandardButtons(QMessageBox::Ok);
            box.setButtonText(QMessageBox::Ok, "确认");
            box.exec();
            return;
        }
        string setFreqRegion = m_readerMethod->SetFrequencyRegion(g_btReadId, freqregion, startFreq, endFreq);
        if(setFreqRegion.length() == 0){
            return;
        }
        m_TcpSocket->write((const char*)setFreqRegion.c_str(), setFreqRegion.length());
    }
}

void UHF_RFID_Reader::on_btnGetLinkPro_clicked()
{
    string getRadioProStr = m_readerMethod->GetRadioProfile(g_btReadId);
    if(getRadioProStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)getRadioProStr.c_str(), getRadioProStr.length());
}

void UHF_RFID_Reader::on_btnSetLinkPro_clicked()
{
    string setRadioProStr = m_readerMethod->SetRadioProfile(g_btReadId, m_profileId);
    if(setRadioProStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)setRadioProStr.c_str(), setRadioProStr.length());
}

void UHF_RFID_Reader::on_btnChooseTag_clicked()
{
    QString tagHexStr = ui->cbBox_labelList->currentText();
    string str = tagHexStr.toStdString();
    int begin = 0;
    begin = str.find(" ", begin);
    while(begin != -1)
    {
        str.replace(begin, 1, "");
        begin = str.find(" ", begin);
    }
    // qDebug() << QString::fromStdString(str);
    /*
    if(str.length() != )
    {
        QMessageBox box(QMessageBox::Warning, "错误", "标签数据有问题，请选择其他标签! ");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }*/
    char tagstr[128] = {0};
    unsigned char tag[64] = {0};
    int nLen = 64;
    memcpy( tagstr, str.c_str(), str.length());
    HexToChar(tagstr, str.length(), (char*)tag, nLen);
    ui->lineEdit_17->setText(tagHexStr);
    ui->checkBox_tagc->setCheckState(Qt::Checked);
    /*
    memset(tagstr, 0, 36);
    int i = 0;
    char* ptr = tagstr;
    for(i = 0; i<12; i++)
    {
        sprintf(ptr, "%02X ", tag[i]);
        ptr += 3;
    }*/
    //qDebug() << QString(tagstr);

    int epclen = str.length()/2;
    unsigned char btMode = 0x00;
    string getAccessEpcStr = m_readerMethod->GetAccessEpcMatch(g_btReadId);
    if(getAccessEpcStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)getAccessEpcStr.c_str(), getAccessEpcStr.length());
    string cancelAccessEpcStr = m_readerMethod->CancelAccessEpcMatch(g_btReadId, tag, epclen, btMode);
    if(cancelAccessEpcStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)cancelAccessEpcStr.c_str(), cancelAccessEpcStr.length());

}

void UHF_RFID_Reader::on_btnReadTag_clicked()
{
    char passwordStr[8] = {0};
    string str = ui->lineEdit_password->text().toStdString();
    int begin = 0;
    begin = str.find(" ", begin);
    while(begin != -1)
    {
        str.replace(begin, 1, "");
        begin = str.find(" ", begin);
    }
    if( ui->lineEdit_password->text().isEmpty() )
    {
        QMessageBox box(QMessageBox::Warning, "错误", "密码不得为空! ");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    if(str.length() != 8)
    {
        QMessageBox box(QMessageBox::Warning, "错误", "输入的密码字符串有误! ");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    memcpy(passwordStr, str.c_str(), 8);
    unsigned char pass[4] = {0};
    int n = 4;
    HexToChar((char*)passwordStr, 8, (char*)pass, n);
    unsigned char wordAdd = 0x00;
    if( ui->lineEdit_start->text().isEmpty() )
    {
        QMessageBox box(QMessageBox::Warning, "错误", "请输入起始地址!");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    if( ui->lineEdit_dataLen->text().isEmpty() )
    {
        QMessageBox box(QMessageBox::Warning, "错误", "请输入读取长度! ");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    wordAdd = (unsigned char)ui->lineEdit_start->text().toInt();
    unsigned char wordCnt;
    wordCnt = (unsigned char)ui->lineEdit_dataLen->text().toInt();

    string readTagStr = m_readerMethod->ReadTag(g_btReadId, m_memBank, wordAdd, wordCnt);
    if(readTagStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)readTagStr.c_str(), readTagStr.length());
}

void UHF_RFID_Reader::on_btnReadSet_clicked()
{
    if(ui->setIdetifier->text().isEmpty())
    {
        QMessageBox box(QMessageBox::Warning, "错误", "请输入十六进制字符串! ");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    string str = ui->setIdetifier->text().toStdString();

    char re[24] = {0};
    int begin = 0;
    begin = str.find(" ", begin);
    while(begin != -1)
    {
        str.replace(begin, 1, "");
        begin = str.find(" ", begin);
    }
    if (str.length() != 24)
    {
        QMessageBox box(QMessageBox::Warning, "错误", "输入有误! ");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    memcpy(re, str.c_str(), 24);
    int len = 12;
    unsigned char identifier[12] = {0};
    HexToChar(re, 24, (char*)identifier, len );

    string setReadIdentStr = m_readerMethod->SetReaderIdentifier(g_btReadId, identifier);
    if(setReadIdentStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)setReadIdentStr.c_str(), setReadIdentStr.length());

}
//设置蜂鸣器
void UHF_RFID_Reader::on_btnBuzzerSet_clicked()
{
    string setBeeperModeStr = m_readerMethod->SetBeeperMode(g_btReadId, m_beeper);
    if(setBeeperModeStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)setBeeperModeStr.c_str(), setBeeperModeStr.length());
}

void UHF_RFID_Reader::on_btnWriteLalel_clicked()
{
    char passwordStr[8] = {0};
    string str = ui->lineEdit_password->text().toStdString();
    int begin = 0;
    begin = str.find(" ", begin);
    while(begin != -1)
    {
        str.replace(begin, 1, "");
        begin = str.find(" ", begin);
    }
    if( ui->lineEdit_password->text().isEmpty() )
    {
        QMessageBox box(QMessageBox::Warning, "错误", "密码不得为空! ");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    if(str.length() != 8)
    {
        QMessageBox box(QMessageBox::Warning, "错误", "输入的密码字符串有误! ");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    memcpy(passwordStr, str.c_str(), 8);
    unsigned char pass[4] = {0};
    int n = 4;
    HexToChar((char*)passwordStr, 8, (char*)pass, n);
    short int wordAdd;
    if( ui->lineEdit_start->text().isEmpty() )
    {
        QMessageBox box(QMessageBox::Warning, "错误", "请输入起始地址!");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }/*
    if( ui->lineEdit_dataLen->text().isEmpty() )
    {
        QMessageBox box(QMessageBox::Warning, "错误", "请输入写入长度! ");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }*/
    wordAdd = (unsigned char)ui->lineEdit_start->text().toShort();
    unsigned char wordCnt;
    wordCnt = (unsigned char)ui->lineEdit_dataLen->text().toInt();

    string inData = ui->lineEdit_writeData->text().toStdString();
    cutStrZero(inData);
    char ind[64] = {0};
    HexToChar((char*)inData.c_str(), inData.length(), ind, n);

    string writeTagStr = m_readerMethod->WriteTag(g_btReadId, pass, m_memBank, (unsigned char*)&wordAdd, wordCnt, (unsigned char*)ind, str.length()/2);
    if(writeTagStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)writeTagStr.c_str(), writeTagStr.length());

}

void UHF_RFID_Reader::on_pushButton_26_clicked()
{
    string str = ui->lineEdit_27->text().toStdString();
    cutStrZero(str);
    if( str.length() != 8 ){
        QMessageBox box(QMessageBox::Warning, "错误", "填写的密码有误");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    unsigned char password[4];
    int n = 0;
    memset(password, 0, 4);
    HexToChar((char*)str.c_str(), str.length(), (char*)password, n);
    string lockTagStr = m_readerMethod->LockTag(g_btReadId, password, m_memBankLock, m_lockType);
    if(lockTagStr.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)lockTagStr.c_str(), lockTagStr.length());
}


void UHF_RFID_Reader::on_btnRefresh_clicked()
{
    m_nTagNum1 = 0;
    m_nTagNum2 = 0;
    m_nBackNum1 = 0;
    m_nBackNum2 = 0;
    m_lTotalTime1 = 0;
    m_lTotalTime2 = 0;

    ui->lineEdit_WorkTemp->clear();
    ui->lineEdit_Version->clear();
    ui->lineEdit_ReaderIdentifier->clear();
    ui->setIdetifier->clear();
    ui->lineEdit_OutputPower->clear();
    ui->lineEdit_RL->clear();
    ui->lineEdit_antDetector->clear();
    ui->lineEdit_customStart->clear();
    ui->lineEdit_freqGap->clear();
    ui->lineEdit_FreqNum->clear();
    ui->lineEdit_OneInvTimes->setText(QString("1"));
    ui->lcdNum_TagCount->display(0);
    ui->lcdNum_ReadRate->display(0);
    ui->lcdNum_ReadTime->display(0);
    ui->lcdNum_Back->display(0);
    ui->lcdNum_totalTime->display(0);
    ui->minDBM->clear();
    ui->maxDBM->clear();
    ui->tableWidget_1->setRowCount(0);
    ui->tableWidget_1->clearContents();

    ui->lineEdit_18->setText(QString("1"));
    ui->lineEdit_19->setText(QString("1"));
    ui->lineEdit_20->setText(QString("1"));
    ui->lineEdit_21->setText(QString("1"));
    ui->lineEdit_22->setText(QString("0"));
    ui->lineEdit_23->setText(QString("10"));

    ui->lcdNum_TagCount1->display(0);
    ui->lcdNum_ReadRate1->display(0);
    ui->lcdNum_ReadTime1->display(0);
    ui->lcdNum_Back1->display(0);
    ui->lcdNum_totalTime1->display(0);
    ui->tableWidget_2->setRowCount(0);
    ui->tableWidget_3->clearContents();
    ui->lineEdit_17->clear();
    ui->checkBox_tagc->setChecked(false);

    ui->lineEdit_password->clear();
    ui->lineEdit_start->clear();
    ui->lineEdit_dataLen->clear();
    ui->lineEdit_writeData->clear();
    ui->lineEdit_27->clear();
    ui->lineEdit_dsPass->clear();
    ui->tableWidget_3->setRowCount(0);
    ui->tableWidget_3->clearContents();

}


void UHF_RFID_Reader::on_btnDestoryLabel_clicked()
{
    string str = ui->lineEdit_dsPass->text().toStdString();
    cutStrZero(str);
    if(str.length() != 8)
    {
        QMessageBox box(QMessageBox::Warning, "错误", "填写的密码有误");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    char password[4];
    int n = 0;
    memset(password, 0, 4);
    HexToChar((char*)str.c_str(), str.length(), password, n);
    string strKillTag = m_readerMethod->KillTag(g_btReadId, (unsigned char*)password);
    if(strKillTag.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)strKillTag.c_str(), strKillTag.length());

}


void UHF_RFID_Reader::on_readGpio_clicked()
{
    string str = m_readerMethod->ReadGpioValue(g_btReadId);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::on_setGpio3_clicked()
{
    unsigned char chooseGpio = 0x03;
    unsigned char gpioValue;
    if(ui->gpio3_high->isChecked())
    {
        gpioValue = 0x01;
    }
    else if ( ui->gpio3_low->isChecked() )
    {
        gpioValue = 0x00;
    }
    else
    {
        QMessageBox box(QMessageBox::Warning, "错误", "请选择GPIO状态");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    string str = m_readerMethod->WriteGpioValue(g_btReadId, chooseGpio, gpioValue);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::on_setGpio4_clicked()
{
    unsigned char chooseGpio = 0x04;
    unsigned char gpioValue;
    if(ui->gpio4_high->isChecked())
    {
        gpioValue = 0x01;
    }
    else if( ui->gpio4_low->isChecked() )
    {
        gpioValue = 0x00;
    }
    else
    {
        QMessageBox box(QMessageBox::Warning, "错误", "请选择GPIO状态");
        box.setStandardButtons(QMessageBox::Ok);
        box.setButtonText(QMessageBox::Ok, "确认");
        box.exec();
        return;
    }
    string str = m_readerMethod->WriteGpioValue(g_btReadId, chooseGpio, gpioValue);
    if(str.length() == 0){
        return;
    }
    m_TcpSocket->write((const char*)str.c_str(), str.length());
}

void UHF_RFID_Reader::closeEvent(QCloseEvent * event)
{
    /*
    m_Inventory = false;
    m_isInventory = false;
    m_isInventoryFast = false;
    if( m_btcpInit == true )
    {
        delete m_TcpSocket;
    }
*/
    // on_btnRefresh_clicked();
    on_btnStopInventory_clicked();
    on_btnStopFastInventory_clicked();
    on_btnDisconnect_Tcp_clicked();
    on_btnRefresh_clicked();
    // qDebug() << "this is closeEvent";
    /*
    QMessageBox box(QMessageBox::Warning, "错误", "确认退出程序？");
    box.setStandardButtons(QMessageBox::Ok);
    box.setButtonText(QMessageBox::Ok, "确认");
    box.exec();

*/

    WaitSec(500);


    emit closeWindow();
    // emit closeWindow();

}


void UHF_RFID_Reader::on_btnTest_clicked(){

    // Socket连接读写器
    QString ip = "192.168.0.178";
    int port = 4001;

    QHostAddress address;
    address.setAddress(ip);

    t_TcpSocket->abort();
    t_TcpSocket->connectToHost(address, port);

    QByteArray buffer = t_TcpSocket->readAll();
    WaitSec(10);
    connect(t_TcpSocket, SIGNAL(readyRead()), this, SLOT(On_tTcpSocket_readyRead()));


    // 设置配置参数
    unsigned char readId = 0x01;    //读写器地址
    unsigned char workAnt = 0x00;   //工作天线
    unsigned char round = 0x01;     //盘存过程重复次数


    // 生成命令
    string cmdSetAnt = m_readerMethod->SetWorkAntenna(readId, workAnt);
    string cmdInventoryReal = m_readerMethod->InventoryReal(readId, round);

    // 调试信息
    QString debugStr = QString::fromStdString(cmdSetAnt);
    QByteArray byteArr;
    qDebug()<<"SetWorkAnt command:"<<byteArr.append(debugStr).toHex();

    debugStr =QString::fromStdString(cmdInventoryReal);
    byteArr.clear();
    qDebug()<<"InventoryReal command:"<<byteArr.append(debugStr).toHex();


    while(true){

        //设置天线
        if(cmdSetAnt.length() == 0){
            return;
        }
        t_TcpSocket->write((const char*)cmdSetAnt.c_str(), cmdSetAnt.length());
        WaitSec(30);

        //盘存标签
        if(cmdInventoryReal.length() == 0){
            return;
        }
        t_TcpSocket->write((const char*)cmdInventoryReal.c_str(), cmdInventoryReal.length());
    }
}

void UHF_RFID_Reader::On_tTcpSocket_connected(){
    qDebug()<<"tcp socket connected";

    t_connected = true;
}

void UHF_RFID_Reader::On_tTcpSocket_readyRead(){

    unsigned char data[MAX_RECV_SOCKET_BUFFER] = {0};
    int length = t_TcpSocket->read((char*)data, MAX_RECV_SOCKET_BUFFER-1);

    if (length <= 0)
    {
        return;
    }

    list<string> dataList;

    //处理数据
    c_pDealData->DecodeThenDealMessage(length, data, dataList);

    if(dataList.empty()){
        return;
    }


    list<string>::iterator it;
    for( it = dataList.begin(); it != dataList.end(); ++it )
    {
        BackMessageData msgData = c_pDealData->DealMsg((unsigned char*)it->c_str(), it->length() );
        if( msgData.operaterSuccess != COMMAND_SUCCESS ){
            continue;
        }
        switch (msgData.type) {
        case 0x89:
        {
            RealInventoryData data;
            memset(&data, 0, sizeof(RealInventoryData));
            memcpy(&data, msgData.msgStr, msgData.msgLen );
            if( data.dataType == 0 )
            {
                qDebug()<<"count";
                //                emit Signal_ShowInventoryData(data.countData);
            }
            else
            {
                qDebug()<<"epc";

                EpcData epcData=data.epcData;

                MapEpcData mapepc;
                memset(&mapepc, 0, sizeof(MapEpcData));

                mapepc.epcLen = epcData.epcLen;
                mapepc.freqPoint = epcData.freqPoint;
                mapepc.rssi = epcData.rssi;
                memcpy(mapepc.pc, epcData.pc, 2 );
                memcpy(mapepc.epc, epcData.epc, epcData.epcLen );


                char pc[7] = {0};
                if(mapepc.epcLen > 1024)
                {
                    return;
                }

                char *epcStr = new char[mapepc.epcLen*3+1];
                char *p = epcStr;
                memset(epcStr, 0, mapepc.epcLen*3+1);
                for(int i=0; i<epcData.epcLen; i++)
                {
                    sprintf(p, "%02X ", (char*)mapepc.epc[i]);
                    p += 3;
                }
                sprintf(pc, "%02X %02X", (char*)mapepc.pc[0], (char*)mapepc.pc[1]);

                QString epc = QString(QLatin1String(epcStr));
                QString ip = QString(QLatin1String(pc));
                qDebug()<<"epc:"<<epc<<",ip:"<<ip;

                delete[] epcStr;
            }
            break;
        }

        default:
            break;
        }
    }
    dataList.clear();

}



